<template>
  <div class="list" ref="warpper">
    <div>
      <div class="area">
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button">
              <!-- {{this.$store.state.city}} -->
              {{this.currentCity}}
            </div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div
            class="button-wrapper"
            v-for="item of hot"
            :key="item.id"
            @click="handleCityClick(item.name)"
          >
            <div class="button">{{item.name}}</div>
          </div>
        </div>
      </div>
      <div class="area" v-for="(item,key) of cities" :key="key">
        <div class="title border-topbottom">{{key}}</div>
        <div
          class="item-list"
          @click="handleCityClick(inneritem.name)"
          v-for="inneritem of item "
          :key="inneritem.id"
        >
          <div class="item border-bottom">{{inneritem.name}}</div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import BScroll from "better-scroll";

import { mapState, mapMutations } from "vuex";
export default {
  name: "CityList",
  props: {
    hot: Array,
    cities: Object
  },
  data() {
    return {};
  },
  methods: {
    handleCityClick(city) {
      //  console.log("handleCityClick:"+city)
      // this.$store.dispatch('changeCity',city);// 组件通过Actions 调用Commit
      //this.$store.commit("changeCity", city); //通过vuex直接调用Commit
      this.changeCity(city);//通过map Mutations 映射调用
      this.$router.push("/"); //路由页面跳转
    },
    ...mapMutations(["changeCity"]) //将vuex -> Mutations中的changeCity方法映射到当前组件
  },
  mounted() {
    //使用了better-scroll，默认它会阻止touch事件。所以在配置中需要加上click: true
    this.scroll = new BScroll(this.$refs.warpper, {
      mouseWheel: false,
      click: true,
      tap: true
    });
  },
  computed: {
    ...mapState({
      currentCity: "city" //将vuex中city共用数据映射到当前组件中的计算属性中，映射过来的名称是currentCity
    })
  }
};
</script>
<style lang="stylus" scoped>
@import '~styles/varibles.styl';

.list {
  text-align: left;
  overflow: hidden;
  position: absolute;
  top: 1.6rem;
  left: 0;
  right: 0;
  bottom: 0;
}

.border-topbottom {
  &:before {
    border-color: #ccc;
  }

  &:after {
    border-color: #ccc;
  }
}

.border-bottom {
  &:before {
    border-color: #ccc;
  }
}

.title {
  line-height: 0.54rem;
  background: #eee;
  padding-left: 0.2rem;
  color: #666;
  font-size: 0.26rem;
}

.button-list {
  padding: 0.1rem 0.6rem 0.1rem 0.1rem;
  overflow: hidden;

  .button-wrapper {
    float: left;
    width: 33.33%;

    .button {
      margin: 0.1rem;
      padding: 0.1rem 0;
      text-align: center;
      border-radius: 0.06rem;
      border: 0.02rem solid #ccc;
    }
  }
}

.item-list {
  .item {
    line-height: 0.76rem;
    padding-left: 0.2rem;
  }
}
</style>



